/*
 * Console.cpp
 *
 *  Created on: Jul 17, 2012
 *      Author: The Almighty Eric
 */

#include "Console.h"

Console::Console()
{
	command = "";
	contents = std::list<std::string>();
}

void Console::Issue(GameMap *m, engine *e, int *gridw, int *gridh)
{
	contents.push_back(command);

	std::list<std::string> params = std::list<std::string>();

	parsecommand(&command, &params);

	std::list<std::string>::iterator it = params.begin();

	if(command == "name")
	{
		//Set map name
		m->name = *it;
	}
	else if(command == "grid")
	{
		//Set grid dimensions
		*gridw = atoi(it->c_str());
		it++;
		*gridh = atoi(it->c_str());

		if(*gridh < 5 || *gridw < 5)
		{
			*gridw = 30;
			*gridh = 30;
		}
	}
	else if(command == "audio")
	{
		m->audio = *it;
		//Set map audio
	}
	else if(command == "save")
	{
		//Save map as
	}
	else if(command == "load")
	{
		//Load specified map
	}
	else if(command == "test")
	{
		//Playtest map
	}
	else if(command == "size")
	{
		//Set Map size
		m->w = atoi(it->c_str());
		it++;
		m->h = atoi(it->c_str());
	}
	else if(command == "font")
	{
		std::string f = *it;
		it++;
		e->setFont(f, atoi(it->c_str()));
	}
	else if(command == "background")
	{
		m->background = *it;
	}
	else if(command == "display")
	{
		std::string temp = "";
		//display map properties
		contents.push_back("Map Properties:");
		contents.push_back(("Name: "+m->name));
		temp = "Width: ";
		temp += itoa(m->w);
		/*char buffer[10];
		itoa(m->w, buffer, 10);
		temp += buffer;*/
		contents.push_back(temp);
		temp = "Height: ";
		temp += itoa(m->h);
		/*itoa(m->h, buffer, 10);
		temp += buffer;*/
		contents.push_back(temp);
		contents.push_back(("Audio: "+m->audio));
		contents.push_back(("Background: "+m->background));
		temp = "Grid Height: ";
		temp += itoa(*gridh);
		/*itoa(*gridh, buffer, 10);
		temp += buffer;*/
		contents.push_back(temp);
		temp = "Grid Width: ";
		temp += itoa(*gridw);
		/*itoa(*gridw, buffer, 10);
		temp += buffer;*/
		contents.push_back(temp);
	}
	else if(command == "showlayer")
	{
		int temp;
		while(it != contents.end())
		{
			temp = atoi(it->c_str());
			e->showLayer(temp);
		}
	}
	else if(command == "hidelayer")
	{
		int temp;
		while(it != contents.end())
		{
			temp = atoi(it->c_str());
			e->hideLayer(temp);
		}
	}
	else if(command == "clear")
		contents.clear();

	//Reset command string
	command = "";

	while(contents.size() > 39)
	{
		contents.pop_front();
	}
}

void Console::parsecommand(std::string *com, std::list<std::string> *params)
{
	std::string comtemp = "";
	std::string paramtemp = "";
	bool p = false;

	for(int x = 0; x < com->length(); x++)
	{
		if(com->at(x) != ' ' && p == false)
		{
			if(com->at(x) == '-')
			{
				p = true;
			}
			if(com->at(x) != '-')
				comtemp += com->at(x);
		}
		if(p)
		{
			if(com->at(x) == '-')
			{
				if(paramtemp != "")
					params->push_back(paramtemp);
				paramtemp = "";
			}
			else
				paramtemp += com->at(x);

			if(x == com->length()-1)
				params->push_back(paramtemp);
		}
	}
	*com = comtemp;
}
